package cn.itsource.campus.controller;

import cn.itsource.campus.domain.User;
import cn.itsource.campus.exception.AccountException;
import cn.itsource.campus.exception.PasswordException;
import cn.itsource.campus.service.IUserService;
import cn.itsource.campus.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        try {
            return userService.checkUserByUsernameAndPassword(user.getUsername(), user.getPassword());
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        }catch (AccountException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (PasswordException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        }
    }
}
